package cn.wolfcode.rocketmq.boot;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
public class HelloController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("01-hello")
    public String sendMsg(String message,String age) throws Exception{
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        Map<String,Object> map=new HashMap<>();
        //用户自定义属性
        map.put("age", age);
        map.put("name", "hesj");
        //也可以设置系统属性
        map.put(MessageConst.PROPERTY_KEYS,age);
        rocketMQTemplate.convertAndSend("01-boot-hello:TagB",message,map);
        rocketMQTemplate.syncSend("01-boot-hello", message);
        rocketMQTemplate.asyncSend("01-boot-hello", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息消费成功");
            }

            @Override
            public void onException(Throwable e) {
                System.out.println("消息消费失败");
            }
        });
        rocketMQTemplate.sendOneWay("01-boot-hello", message);
        DefaultMQProducer producer1 = rocketMQTemplate.getProducer();
        rocketMQTemplate.syncSend("01-boot-hello", MessageBuilder.withPayload(message).build(), 3000, 3);


        Message<?> msg = MessageBuilder.withPayload(message).setHeader(MessageConst.PROPERTY_KEYS, "1100").build();
        rocketMQTemplate.send("01-boot-hello", msg);
        return "success";
    }
}
